package com.zy.algorithm.v3.sort

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.FrameLayout
import android.widget.TextView
import com.zy.algorithm.v3.R
import com.zy.view.DIYSortView
import com.zy.utils.data.Point
import com.zy.utils.data.auto.AutoIndexElement
import com.zy.utils.data.auto.AutoNodeStep
import com.zy.utils.data.auto.Coordinate
import com.zy.zlog.ZLog
import java.util.ArrayList
import java.util.Random

class RadixSortFragment : BaseV3SortFragment() {
    private val array = intArrayOf(783, 299, 469, 8, 439, 487, 165, 872, 604, 871)

    private lateinit var sortOpContainer: FrameLayout
    private lateinit var radixDes: TextView
    private lateinit var radix_op_auto1: Button
    private lateinit var radix_op_auto10: Button
    private lateinit var radix_op_auto100: Button

    override val layout: Int
        get() = R.layout.fragment_sort_radix

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        sortOpContainer = view.findViewById(R.id.sort_op_container)
        radixDes = view.findViewById(R.id.radix_des)
        view.findViewById<Button>(R.id.radix_op_1).setOnClickListener {
            hideOpContainer()
            mainRunPost(1000) {
                radixDes.text = "基数:个位"
                updateColorShowIndex(diyV4, 0)
            }
        }

        view.findViewById<Button>(R.id.radix_op_10).setOnClickListener {
            hideOpContainer()
            mainRunPost(1000) {
                radixDes.text = "基数:十位"
                updateColorShowIndex(diyV4, 1)
            }
        }

        view.findViewById<Button>(R.id.radix_op_100).setOnClickListener {
            hideOpContainer()
            mainRunPost(1000) {
                radixDes.text = "基数:百位"
                updateColorShowIndex(diyV4, 2)
            }
        }

        radix_op_auto1 = view.findViewById<Button>(R.id.radix_op_auto1)
        radix_op_auto1.setOnClickListener {
            hideOpContainer()
            radixDes.text = "基数:个位"
            mainRunPost(1000) {
                updateColorShowIndex(diyV4, 0)
                autoMoveStepByLockUnit(diyV4)
            }
        }

        radix_op_auto10 = view.findViewById<Button>(R.id.radix_op_auto10)
        radix_op_auto10.setOnClickListener {
            hideOpContainer()
            radixDes.text = "基数:十位"
            mainRunPost(1000) {
                updateColorShowIndex(diyV4, 1)
                autoMoveStepByLockTen(diyV4)
            }
        }


        radix_op_auto100 = view.findViewById<Button>(R.id.radix_op_auto100)
        radix_op_auto100.setOnClickListener {
            hideOpContainer()
            radixDes.text = "基数:百位"

            mainRunPost(1000) {
                updateColorShowIndex(diyV4, 2)
                autoMoveStepByLockHun(diyV4)
            }
        }

        super.onViewCreated(view, savedInstanceState)
    }

    private fun hideOpContainer() {
        ZLog.d("RadixSortFragment", "hideOpContainer")
        sortOpContainer.visibility = View.GONE
    }

    private fun hideOp() {
        radix_op_auto1.visibility = View.GONE
        radix_op_auto10.visibility = View.GONE
        radix_op_auto100.visibility = View.GONE
    }

    override fun setSortData(diyV4: DIYSortView) {
        if (code == 0) {
            for (i in array.indices) {
                array[i] = Random().nextInt(1000)
            }
            hideOp()
        }

        diyV4.setD(150)
        diyV4.create(3, 2, array, Point.Type.DEFAULT)
        diyV4.addPoint(3, 1, "V")

        val assistArray = intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
        diyV4.addNoMoveAssistList(3, 6, assistArray, Point.Type.ASSIST)
    }

    override fun autoMove(diyV4: DIYSortView) {
        super.autoMove(diyV4)
        sortOpContainer.visibility = View.VISIBLE
    }

    //自动动画(个位)
    @Deprecated("比较麻烦")
    private fun autoMoveStepOne(diyV4: DIYSortView) {
        val baseX = 3
        val list: MutableList<AutoNodeStep> = ArrayList<AutoNodeStep>()
        val baseY = intArrayOf(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)

        var baseAssBaseX = 3

        array.forEachIndexed { index, i ->
            val curV = i % 10

            val autoIndexElement = AutoIndexElement(index, Coordinate(baseX + curV, baseY[curV]--))
            val assElement = AutoIndexElement(Point.ASS_INDEX, Coordinate(++baseAssBaseX, 1))
            list.add(AutoNodeStep(listOf(autoIndexElement, assElement)))
        }
        diyV4.autoMove(list)
    }


    //783, 299, 469, 8, 439, 487, 165, 872, 604, 871
    private fun autoMoveStepByLockUnit(diyV4: DIYSortView) {
        var baseX = 3
        val list: MutableList<AutoNodeStep> = ArrayList<AutoNodeStep>()
        val baseY = intArrayOf(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)

        var baseAssBaseX = 3

        array.forEachIndexed { index, i ->
            val curV = i % 10
            val autoIndexElement = AutoIndexElement(index, Coordinate(baseX + curV, baseY[curV]--))
            val assElement = AutoIndexElement(Point.ASS_INDEX, Coordinate(baseAssBaseX++, 1))
            list.add(AutoNodeStep(listOf(autoIndexElement, assElement)))
        }

        //个位回
        baseX = 12
        val indexArray = listOf<Int>(4, 2, 1, 3, 5, 6, 8, 0, 7, 9)
        indexArray.forEach { index ->
            val autoIndexElement = AutoIndexElement(index, Coordinate(baseX--, 2))
            val assElement = AutoIndexElement(Point.ASS_INDEX, Coordinate(--baseAssBaseX, 1))
            list.add(AutoNodeStep(listOf(autoIndexElement, assElement)))
        }

        diyV4.autoMove(list)
    }


    private fun autoMoveStepByLockTen(diyV4: DIYSortView) {
        var baseX = 3
        val list: MutableList<AutoNodeStep> = ArrayList<AutoNodeStep>()
        val baseY = intArrayOf(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)

        var baseAssBaseX = 3

        var indexArray = listOf(9, 7, 0, 8, 6, 5, 3, 1, 2, 4)


        indexArray.forEach { pointIndex ->
            val point = diyV4.findNodeByIndex(pointIndex)
            val v = point?.v ?: "000"
            //获取当前Node的十位的数字

            var curV = 0
            if (v.length == 2) {
                curV = v[0] - '0'
            } else if (v.length == 3) {
                curV = v[1] - '0'
            }

            val autoIndexElement =
                AutoIndexElement(pointIndex, Coordinate(baseX + curV, baseY[curV]--))
            val assElement = AutoIndexElement(Point.ASS_INDEX, Coordinate(baseAssBaseX++, 1))
            list.add(AutoNodeStep(listOf(autoIndexElement, assElement)))
        }

        //十位回
        baseX = 12
        indexArray = listOf<Int>(1, 5, 0, 7, 9, 2, 6, 4, 3, 8)
        indexArray.forEach { index ->
            val autoIndexElement = AutoIndexElement(index, Coordinate(baseX--, 2))
            val assElement = AutoIndexElement(Point.ASS_INDEX, Coordinate(--baseAssBaseX, 1))
            list.add(AutoNodeStep(listOf(autoIndexElement, assElement)))
        }

        diyV4.autoMove(list)
    }


    private fun autoMoveStepByLockHun(diyV4: DIYSortView) {
        var baseX = 3
        val list: MutableList<AutoNodeStep> = ArrayList<AutoNodeStep>()
        val baseY = intArrayOf(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)

        var baseAssBaseX = 3

        var indexArray = listOf(8, 3, 4, 6, 2, 9, 7, 0, 5, 1)


        indexArray.forEach { pointIndex ->
            val point = diyV4.findNodeByIndex(pointIndex)
            val v = point?.v ?: "000"
            //获取当前Node的十位的数字

            var curV = 0
            if (v.length == 3) {
                curV = v[0] - '0'
            }

            val autoIndexElement =
                AutoIndexElement(pointIndex, Coordinate(baseX + curV, baseY[curV]--))
            val assElement = AutoIndexElement(Point.ASS_INDEX, Coordinate(baseAssBaseX++, 1))
            list.add(AutoNodeStep(listOf(autoIndexElement, assElement)))
        }

        //十位回
        baseX = 12
        indexArray = listOf<Int>(7, 9, 0, 8, 5, 2, 4, 1, 6, 3)
        indexArray.forEach { index ->
            val autoIndexElement = AutoIndexElement(index, Coordinate(baseX--, 2))
            val assElement = AutoIndexElement(Point.ASS_INDEX, Coordinate(--baseAssBaseX, 1))
            list.add(AutoNodeStep(listOf(autoIndexElement, assElement)))
        }

        diyV4.autoMove(list)
    }


    //基数排序 改变对应位数的颜色
    private fun updateColorShowIndex(diyV4: DIYSortView, index: Int) {
//        diyV4.getPointList().forEach {
//            if (it is DifferencePoint) {
//                it.updateIndex(index)
//            }
//        }
        diyV4.invalidate()
    }
}